package com.xzt.meeting.dao;

import com.xzt.meeting.entity.Meeting;
import org.springframework.dao.EmptyResultDataAccessException;
import org.springframework.jdbc.core.BeanPropertyRowMapper;

import java.util.List;

/**
 * @Author xzt
 * @Date 2022/5/8 21:24
 * @Version 1.0
 * @Introduce
 */
public class MeetingDao extends BaseDao{
    public List<Meeting> findPage(String title, Integer status, Integer startIndex, Integer pageSize) {
        if (status == -1){
            return jdbcTemplate.query("SELECT * FROM `meeting` where title like ? order by publish_date desc limit ?,?",
                    new BeanPropertyRowMapper<>(Meeting.class),"%"+title+"%",startIndex,pageSize);
        }else {
            return jdbcTemplate.query("SELECT * FROM `meeting` where title like ? and `status`=? " +
                    "order by publish_date desc limit ?,?",
                    new BeanPropertyRowMapper<>(Meeting.class),"%"+title+"%",status,startIndex,pageSize);
        }
    }

    public Integer countBytitleAndstatus(String title, Integer status) {
        try{
            if (status == -1){
                return jdbcTemplate.queryForObject("SELECT count(*) FROM `meeting` where title like ?",Integer.class,"%"+title+"%");
            }else {
                return jdbcTemplate.queryForObject("SELECT count(*) FROM `meeting` where title like ? and `status`=?",Integer.class,"%"+title+"%",status);
            }
        }catch (EmptyResultDataAccessException e){
            return null;
        }
    }

    public void save(Meeting meeting) {
        jdbcTemplate.update("insert into meeting values(null,?,?,?,?,?,?,?,?,?)",
                meeting.getDeptName(),
                meeting.getDeptId(),
                meeting.getTitle(),
                meeting.getContent(),
                meeting.getPublishDate(),
                meeting.getStartTime(),
                meeting.getEndTime(),
                meeting.getStatus(),
                meeting.getMakeUser()
        );
    }

    public Meeting findMeetingById(Long meetingId) {
        try{
            return jdbcTemplate.queryForObject("select * from meeting where id=?",
                    new BeanPropertyRowMapper<>(Meeting.class),meetingId);
        }catch (EmptyResultDataAccessException e){
            return null;
        }
    }

    public Integer countJoinMeetingByMeetingId(Long meetingId) {
        try{
            return jdbcTemplate.queryForObject("select count(*) from meeting_join where c_id=?",
                    Integer.class,meetingId);
        }catch (EmptyResultDataAccessException e){
            return null;
        }
    }

    public Integer isJoinMeeting(Long uid, Long meetingId) {
        try{
            return jdbcTemplate.queryForObject("select count(*) from meeting_join where u_id=? and c_id=?",
                    Integer.class,uid,meetingId);
        }catch (EmptyResultDataAccessException e){
            return null;
        }
    }

    public void unJoinMeeting(Long uid, Long mid) {
        jdbcTemplate.update("delete from meeting_join where u_id=? and c_id=?",uid,mid);
    }

    public void joinMeeting(Long uid, Long mid) {
        jdbcTemplate.update("insert into meeting_join values(?,?)",uid,mid);
    }
}
